package com.example.guiguiapplicatio.fragment.home.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.guiguiapplicatio.R;
import com.example.guiguiapplicatio.Response.ServicePackageResponse;

import java.util.ArrayList;
import java.util.List;

public class ServicepacksListAdapter  extends RecyclerView.Adapter<ServicepacksListAdapter.ServicepacksListViewHolder>{
    private List<Integer> selectedPositions = new ArrayList<>();

    private List<ServicePackageResponse> ServicePackageList;
    public ServicepacksListAdapter(List<ServicePackageResponse> ServicePackageList) {
        this.ServicePackageList = ServicePackageList;
    }

    @NonNull
    @Override
    public ServicepacksListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_servicepack, parent, false);
        return new ServicepacksListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ServicepacksListViewHolder holder, int position) {
        // 获取当前位置的服务包数据
        ServicePackageResponse servicePackItem =  ServicePackageList.get(position);

        // 将数据绑定到ViewHolder的视图元素中
        holder.textSequenceNumberTextView.setText(servicePackItem.getId());
        holder.textServicePackageNameTextView.setText(servicePackItem.getName());
        // 还可以根据需要绑定其他视图元素

        // 在这里添加逻辑，处理复选框的状态或其他UI元素的操作
        // 设置CheckBox的状态
        holder.checkBox.setChecked(selectedPositions.contains(position));

        // 为CheckBox添加点击事件监听器
        holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    selectedPositions.add(position);
                } else {
                    selectedPositions.remove((Integer) position);
                }
                notifyDataSetChanged();
            }
        });
    }

    public void setData(List<ServicePackageResponse> data) {
        if (data == null) {
            data = new ArrayList<>();
        }
        this.ServicePackageList = data;
    }
    public List<ServicePackageResponse> getSelectedItems() {
        List<ServicePackageResponse> selectedItems = new ArrayList<>();
        for (Integer position : selectedPositions) {
            selectedItems.add(ServicePackageList.get(position));
        }
        return selectedItems;
    }
    @Override
    public int getItemCount() {
        return ServicePackageList.size();
    }

    public static class ServicepacksListViewHolder extends RecyclerView.ViewHolder {
        TextView textSequenceNumberTextView;
        TextView textServicePackageNameTextView;
        CheckBox checkBox;

        public ServicepacksListViewHolder(@NonNull View itemView) {
            super(itemView);
            textSequenceNumberTextView = itemView.findViewById(R.id.text_sequence_number);
            textServicePackageNameTextView = itemView.findViewById(R.id.text_service_package_name);
            checkBox = itemView.findViewById(R.id.SelectService);
        }
    }
}
